<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Notification;

class NotificationController extends Controller
{
    /**
     * 获取通知列表
     */
    public function index(Request $request)
    {
        $user = Auth::user();
        $type = $request->get('type', 'all');
        $query = $user->notifications();

        if ($type === 'unread') {
            $query->unread();
        } elseif ($type === 'read') {
            $query->read();
        }

        $notifications = $query->paginate(20);

        // 转换通知数据
        $notifications->through(function ($notification) {
            return [
                'id' => $notification->id,
                'type' => $notification->type,
                'title' => $notification->getTitle(),
                'message' => $notification->getMessage(),
                'icon' => $notification->getIcon(),
                'color' => $notification->getColor(),
                'url' => $notification->getUrl(),
                'is_read' => $notification->isRead(),
                'created_at' => $notification->created_at->diffForHumans(),
                'data' => $notification->data,
            ];
        });

        if ($request->wantsJson()) {
            return response()->json($notifications);
        }

        return inertia('Notifications/Index', [
            'notifications' => $notifications,
            'unreadCount' => $user->unread_notification_count,
            'type' => $type,
        ]);
    }

    /**
     * 标记通知为已读
     */
    public function markAsRead(Notification $notification)
    {
        // 确保通知属于当前用户
        if ($notification->notifiable_id !== Auth::id()) {
            abort(403);
        }

        $notification->markAsRead();

        return response()->json([
            'success' => true,
            'message' => '通知已标记为已读',
        ]);
    }

    /**
     * 标记所有通知为已读
     */
    public function markAllAsRead()
    {
        Auth::user()->markAllNotificationsAsRead();

        return response()->json([
            'success' => true,
            'message' => '所有通知已标记为已读',
        ]);
    }

    /**
     * 删除通知
     */
    public function destroy(Notification $notification)
    {
        // 确保通知属于当前用户
        if ($notification->notifiable_id !== Auth::id()) {
            abort(403);
        }

        $notification->delete();

        return response()->json([
            'success' => true,
            'message' => '通知已删除',
        ]);
    }

    /**
     * 批量删除通知
     */
    public function destroyMultiple(Request $request)
    {
        $ids = $request->input('ids', []);

        Auth::user()->notifications()
            ->whereIn('id', $ids)
            ->delete();

        return response()->json([
            'success' => true,
            'message' => '通知已批量删除',
        ]);
    }

    /**
     * 获取未读通知数量
     */
    public function unreadCount()
    {
        return response()->json([
            'count' => Auth::user()->unread_notification_count,
        ]);
    }

    /**
     * 获取最新通知（用于实时更新）
     */
    public function latest()
    {
        $notifications = Auth::user()->notifications()
            ->limit(5)
            ->get()
            ->map(function ($notification) {
                return [
                    'id' => $notification->id,
                    'type' => $notification->type,
                    'title' => $notification->getTitle(),
                    'message' => $notification->getMessage(),
                    'icon' => $notification->getIcon(),
                    'color' => $notification->getColor(),
                    'url' => $notification->getUrl(),
                    'is_read' => $notification->isRead(),
                    'created_at' => $notification->created_at->diffForHumans(),
                ];
            });

        return response()->json($notifications);
    }
}